using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantSubmarineCollisionHandler
/// \brief  Handle the submarine collision reactions
[AddComponentMenu("GK/AbyssalGiant/Submarine/gkAbyssalGiantSubmarineCollisionHandler")]
public class gkAbyssalGiantSubmarineCollisionHandler : MonoBehaviour 
{
	/// \brief  Call at the overlapping beginning
	void OnCollisionEnter(Collision a_rCollisionInfo)
	{
		HandleCollision(a_rCollisionInfo);
	}
	
	/// \brief  Call at the overlapping beginning
	void OnCollisionStay(Collision a_rCollisionInfo)
	{
		HandleCollision(a_rCollisionInfo);
	}
	
	/// \brief  Call at the overlapping beginning
	void OnTriggerEnter(Collider a_rCollisionInfo)
	{
		Vector3 f3ShockDirection;
		gkAbyssalGiantShockEvent rShockEvent;
		
		if(enabled)
		{
			if(a_rCollisionInfo.gameObject.layer == LayerMask.NameToLayer("Rock"))
			{
				// Compute the shock direction
				f3ShockDirection = transform.position - a_rCollisionInfo.gameObject.transform.position;
				f3ShockDirection.z = 0.0f;
				f3ShockDirection.Normalize();
				
				// Send the shock event
				rShockEvent = new gkAbyssalGiantShockEvent(f3ShockDirection);
				GetComponent<gkEventComponent>().RaiseEvent(gkAbyssalGiantShockEvent.ShockBegin, rShockEvent);
			}
		}
	}
	
	/// \brief Handle collision
	void HandleCollision(Collision a_rCollisionInfo)
	{
		Vector3 f3ShockDirection;
		gkAbyssalGiantShockEvent rShockEvent;
		
		if(enabled)
		{
			if(a_rCollisionInfo.gameObject.layer == LayerMask.NameToLayer("Ground"))
			{
				// Compute the shock direction
				f3ShockDirection = Vector3.up;
				
				// Send the shock event
				rShockEvent = new gkAbyssalGiantShockEvent(f3ShockDirection);
				GetComponent<gkEventComponent>().RaiseEvent(gkAbyssalGiantShockEvent.ShockBegin, rShockEvent);
			}
		}
	}
	
	/// Shock End
	public void ShockEnd()
	{
		GetComponent<gkEventComponent>().RaiseEvent(gkAbyssalGiantShockEvent.ShockEnd);
	}
}
